package com.dingup.paike.feign.server.feedback;


import com.dingup.api.Util.DateUtil;
import com.dingup.api.bean.feign.paike.feedback.PkLessonInfoFeignBean;
import com.dingup.api.bean.feign.paike.feedback.PkLessonInfoFeignDTO;
import com.dingup.api.bean.page.PageData;
import com.dingup.paike.bean.dtobean.PkLessonInfoDTO;
import com.dingup.paike.dao.PkLessonInfoMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * Created by wancheng on 2018/10/27.
 */
@RestController()
@RequestMapping("/paike/feign/feedback")
public class PaikeFeedbackFeign {

    @Autowired
    PkLessonInfoMapper pkLessonInfoMapper;

    @RequestMapping(value = "/get_feedback_list_by_studentId_and_class_ids.do", method = RequestMethod.POST)
    public PageData<PkLessonInfoFeignDTO> getFeedbackListByStudentIdAndClassIds(@RequestBody PkLessonInfoFeignBean bean) {
        PageHelper.startPage(bean.getPage(), bean.getRows());
        List<PkLessonInfoDTO> pkLessonInfoDTOs = pkLessonInfoMapper.getPkLessonInfoDTOByStudentIdAndClassId(bean.getStudentId(), bean.getClassIds());
        PageInfo<PkLessonInfoDTO> pageInfo = new PageInfo<>(pkLessonInfoDTOs);
        List<PkLessonInfoFeignDTO> res = new ArrayList<>();
        if (!CollectionUtils.isEmpty(pkLessonInfoDTOs)) {
            res = pageInfo.getList().stream()
                    .map(l -> new PkLessonInfoFeignDTO(
                            l.getSubjectName(),
                            DateUtil.date2Str(l.getBeginTime(), "MM月dd日 HH:mm"),
                            DateUtil.date2Str(l.getEndTime(), "HH:mm"),
                            l.getTeacherName(),
                            l.getClassRoom(),
                            l.getClassId(),
                            l.getUserId(),
                            l.getPkLessonId(),
                            l.getIfOnlineLesson())
                    )
                    .collect(Collectors.toList());
        }
        PageData<PkLessonInfoFeignDTO> pageData = new PageData();
        pageData.setPage(pageInfo.getPageNum());
        pageData.setRows(pageInfo.getPageSize());
        pageData.setTotal(pageInfo.getTotal());
        pageData.setData(res);
        pageData.setTotalPage(pageInfo.getPages());
        return pageData;
    }

}
